const state = {
    layerIndex: {
        baseMap: 0,
        patrolLineLayer: 2,
        initLineLayer: 1,
        patrolPintLayer: 3
    },
    map: ''
}

initMap('map')
state.map.on('click', e => {
    if (e.dragging) {
        return;
    }
    let pixel = state.map.getEventPixel(e.originalEvent);
    let feature = state.map.forEachFeatureAtPixel(pixel, (feature) => {
        return feature;
    });
    if (feature && feature.getGeometry().getType() === 'Point') {
        getPointDetails(feature.A)
    }
})
//获取点位详情
function getPointDetails(info) {
    window.android.openDetails(
        JSON.stringify(info)
    );
}
//经纬度缩放定位
function zoomToPointVideoList(info,data) {
    let lat = info && info.lat ? +info.lat : ''
    let lon = info && info.lon ? +info.lon : ''

    if (lat && lon) {
        state.map.getView().setCenter(wgs84Tomercator([lon, lat]))
        state.map.getView().setZoom(15)

    }

    let pointLayer = new ol.layer.Vector({
        source: new ol.source.Vector(),
        style: feature => {
            let attr = feature.A
            let status = attr.status
            let name = attr.positionName ? attr.positionName : ''
            let imageList = {
                "1": 'images/Patrol.png',//已巡查
                "2": 'images/missPatrol.png',//漏巡查
                "3": 'images/onPatrol.png',//正在巡查
                "4": 'images/noPatrol.png',//未巡查
                "5": 'images/ic_nsgf_map_video_point.png',//视频点位
                "6":'images/ic_nsgf_home_reservoir.png',//区域水库点位
                "7":'images/ic_nsgf_house_map.png',//防汛仓库
            }
            let imgSrc = status && imageList[status] ? imageList[status] : imageList['4']
            //             let imgSrc = 'images/ic_nsgf_map_video_point.png'
            let style = new ol.style.Style({
                image: new ol.style.Icon({
                    scale: 0.55,
                    // anchor: [0.5, 1],
                    opacity: 1,
                    src: imgSrc
                }),
                text: new ol.style.Text({
                    textAlign: 'center',
                    textBaseline: 'middle',
                    font: 'normal 14px Microsoft YaHei-Bold',
                    text: name,
                    offsetY: 25,
                    offsetX: 0,
                    fill: new ol.style.Fill({
                        color: '#000000'
                    }),
                    stroke: new ol.style.Stroke({
                        color: '#ffffff',
                        width: 2.5
                    }),
                    // backgroundFill: new Fill({
                    //   color: 'rgba(0,0,0,0.6)'
                    // }),
                    padding: [5, 5, 5, 5],
                    // declutterMode: "declutter"
                })
            })
            return style
        }
    })
    let group = new ol.layer.Group({
        layers: [pointLayer]
    })
    state.map.getLayers().removeAt(state.layerIndex.patrolPintLayer)
    state.map.getLayers().insertAt(state.layerIndex.patrolPintLayer, group)
    let lintCoor = []
    if (data && data.length > 0) {

        data.forEach(item => {
            let coordinate = wgs84Tomercator([+item.lon, +item.lat])
            lintCoor.push(coordinate)
            let pointFea = new ol.Feature({
                geometry: new ol.geom.Point(coordinate)
            })
            pointFea.setProperties(item)
            pointLayer.getSource().addFeature(pointFea)
        });
    }
    let lineFea = new ol.Feature({
        geometry: new ol.geom.LineString(lintCoor)
    })

    // lineFea.setProperties()
    pointLayer.getSource().addFeature(lineFea)
}

//区域展示全部水库
function zoomToPointProject(info,list) {
    let lat = info && info.lat ? +info.lat : ''
    let lon = info && info.lon ? +info.lon : ''

    if (lat && lon) {
        state.map.getView().setCenter(wgs84Tomercator([lon, lat]))
        state.map.getView().setZoom(15)

    }

    drawInitPint(list)
}

function zoomToPoint(info,zoom) {
    let lat = info && info.lat ? +info.lat : ''
    let lon = info && info.lon ? +info.lon : ''

    if (lat && lon) {
        state.map.getView().setCenter(wgs84Tomercator([lon, lat]))
        state.map.getView().setZoom(zoom)

    }
}
//初始化地图
function initMap(dom) {
    state.map = new ol.Map({
        target: 'map',
        view: new ol.View({
            center: ol.proj.transform([118.98922007041959, 32.15282214704126], 'EPSG:4326', 'EPSG:3857'),
            zoom: 10,
            minZoom: 1,
            maxZoom: 19,
            projection: 'EPSG:3857',
        }),
        layers: getBlankLayers(Object.keys(state.layerIndex).length)
    })


    let baseMap = getBaseMap()
    state.map.getLayers().removeAt(state.layerIndex.baseMap)
    state.map.getLayers().insertAt(state.layerIndex.baseMap, baseMap)

}
function getBaseMap() {
    let vecLayer = getTileLayer_XYZ('vec')
    let cvaLayer = getTileLayer_XYZ("cva")
    let layerGroup = new ol.layer.Group({
        layers: [vecLayer, cvaLayer]
    })
    return layerGroup


}

//绘制巡查线
function drawPatrolLine(data) {
drawPatrolLine(data,false)
}
//绘制巡查线
function drawPatrolLine(data,status) {
    let pointLayer = new ol.layer.Vector({
        source: new ol.source.Vector(),
        style: feature => {
            let attr = feature.A
            let status = attr.status
            let imageList = {
                "1": '../images/Patrol.png',//已巡查
                "2": '../images/missPatrol.png',//漏巡查
                "3": '../images/onPatrol.png',//正在巡查
                "4": '../images/noPatrol.png',//未巡查
            }
            let imgSrc = status && imageList[status] ? imageList[status] : imageList['4']
            // let imgSrc = '../images/noPatrol.png'
            let style = new ol.style.Style({
                image: new ol.style.Icon({
                    scale: 0.55,
                    // anchor: [0.5, 1],
                    opacity: 1,
                    src: imgSrc
                })
            })
            return style
        }
    })
    let lineLayer = new ol.layer.Vector({
        source: new ol.source.Vector(),
        style: feature => {
            // let attr = feature.values_.attribution
            // let sattus = attr.status
            let style = new ol.style.Style({
                stroke: new ol.style.Stroke({
                    color: 'blue',
                    width: 2,
                    lineDash: [3, 4],
                })
            })
            return style
        }
    })
    let group = new ol.layer.Group({
        layers: [lineLayer, pointLayer]
    })
    state.map.getLayers().removeAt(state.layerIndex.patrolLineLayer)
    state.map.getLayers().insertAt(state.layerIndex.patrolLineLayer, group)
    let lintCoor = []
    if (data && data.length >1) {

        data.forEach(item => {
            let coordinate = wgs84Tomercator([+item.lon, +item.lat])
            lintCoor.push(coordinate)
            let pointFea = new ol.Feature({
                geometry: new ol.geom.Point(coordinate)
            })
            pointFea.setProperties(item)
            pointLayer.getSource().addFeature(pointFea)
        });
    }
    let lineFea = new ol.Feature({
        geometry: new ol.geom.LineString(lintCoor)
    })
    // lineFea.setProperties()
    lineLayer.getSource().addFeature(lineFea)
    let extent = lineLayer.getSource().getExtent()
    extent && state.map.getView().fit(extent, {
        // padding:[20,20,20,20]
    })
    if(status){
        let zoom = state.map.getView().getZoom()
        state.map.getView().setZoom(zoom - 1)
    }else{
        state.map.getView().setZoom(15)
    }
}



//区域首页水库点位
function drawInitPintProject(data) {
    let pointLayer = new ol.layer.Vector({
        source: new ol.source.Vector(),
        style: feature => {
            let attr = feature.A
            let status = attr.status
            let name = attr.positionName ? attr.positionName : ''
            let imageList = {
                "1": 'images/Patrol.png',//已巡查
                "2": 'images/missPatrol.png',//漏巡查
                "3": 'images/onPatrol.png',//正在巡查
                "4": 'images/noPatrol.png',//未巡查
                "5": 'images/ic_nsgf_map_video_point.png',//视频点位
                "6":'images/ic_nsgf_home_reservoir.png',//区域水库点位
                "7":'images/ic_nsgf_house_map.png',//防汛仓库
                "77":'images/ic_nsgf_house_big_map.png',//防汛仓库
            }
            let imgSrc = status && imageList[status] ? imageList[status] : imageList['4']
            //             let imgSrc = 'images/ic_nsgf_map_video_point.png'
            let style = new ol.style.Style({
                image: new ol.style.Icon({
                    scale: 0.55,
                    // anchor: [0.5, 1],
                    opacity: 1,
                    src: imgSrc
                }),
                text: new ol.style.Text({
                    textAlign: 'center',
                    textBaseline: 'middle',
                    font: 'normal 14px Microsoft YaHei-Bold',
                    text: name,
                    offsetY: 25,
                    offsetX: 0,
                    fill: new ol.style.Fill({
                        color: '#000000'
                    }),
                    stroke: new ol.style.Stroke({
                        color: '#ffffff',
                        width: 2.5
                    }),
                    // backgroundFill: new Fill({
                    //   color: 'rgba(0,0,0,0.6)'
                    // }),
                    padding: [5, 5, 5, 5],
                    // declutterMode: "declutter"
                })
            })
            return style
        }
    })
    let group = new ol.layer.Group({
        layers: [pointLayer]
    })
    state.map.getLayers().removeAt(state.layerIndex.patrolPintLayer)
    state.map.getLayers().insertAt(state.layerIndex.patrolPintLayer, group)
    let lintCoor = []
    if (data && data.length > 0) {

        data.forEach(item => {
            let coordinate = wgs84Tomercator([+item.lon, +item.lat])
            lintCoor.push(coordinate)
            let pointFea = new ol.Feature({
                geometry: new ol.geom.Point(coordinate)
            })
            pointFea.setProperties(item)
            pointLayer.getSource().addFeature(pointFea)
        });
    }
    let lineFea = new ol.Feature({
        geometry: new ol.geom.LineString(lintCoor)
    })

    // lineFea.setProperties()
    pointLayer.getSource().addFeature(lineFea)
    let extent = pointLayer.getSource().getExtent()
    extent && state.map.getView().fit(extent, {
        // padding:[20,20,20,20]
    })
    let zoom = state.map.getView().getZoom()

    state.map.getView().setZoom(zoom - 1)
}

//绘制初始点位
function drawInitPint(value) {
    let data = JSON.parse(value)
    let pointLayer = new ol.layer.Vector({
        source: new ol.source.Vector(),
        style: feature => {
            let attr = feature.A
            let status = attr.status
            let name = attr.positionName ? attr.positionName : ''
            let imageList = {
                "1": 'images/Patrol.png',//已巡查
                "2": 'images/missPatrol.png',//漏巡查
                "3": 'images/onPatrol.png',//正在巡查
                "4": 'images/noPatrol.png',//未巡查
                "5": 'images/ic_nsgf_map_video_point.png',//视频点位
                "6":'images/ic_nsgf_home_reservoir.png',//区域水库点位
                "66":'images/ic_nsgf_home_big_reservoir.png',//区域水库点位
                "7":'images/ic_nsgf_house_map.png',//防汛仓库
                "77":'images/ic_nsgf_house_big_map.png',//防汛仓库
            }
            let imgSrc = status && imageList[status] ? imageList[status] : imageList['4']
            //             let imgSrc = 'images/ic_nsgf_map_video_point.png'
            let style = new ol.style.Style({
                image: new ol.style.Icon({
                    scale: 0.55,
                    // anchor: [0.5, 1],
                    opacity: 1,
                    src: imgSrc
                }),
                text: new ol.style.Text({
                    textAlign: 'center',
                    textBaseline: 'middle',
                    font: 'normal 14px Microsoft YaHei-Bold',
                    text: name,
                    offsetY: 25,
                    offsetX: 0,
                    fill: new ol.style.Fill({
                        color: '#000000'
                    }),
                    stroke: new ol.style.Stroke({
                        color: '#ffffff',
                        width: 2.5
                    }),
                    // backgroundFill: new Fill({
                    //   color: 'rgba(0,0,0,0.6)'
                    // }),
                    padding: [5, 5, 5, 5],
                    // declutterMode: "declutter"
                })
            })
            return style
        }
    })
    let group = new ol.layer.Group({
        layers: [pointLayer]
    })
    state.map.getLayers().removeAt(state.layerIndex.patrolPintLayer)
    state.map.getLayers().insertAt(state.layerIndex.patrolPintLayer, group)
    let lintCoor = []
    if (data && data.length > 0) {

        data.forEach(item => {
            let coordinate = wgs84Tomercator([+item.lon, +item.lat])
            lintCoor.push(coordinate)
            let pointFea = new ol.Feature({
                geometry: new ol.geom.Point(coordinate)
            })
            pointFea.setProperties(item)
            pointLayer.getSource().addFeature(pointFea)
        });
    }
    let lineFea = new ol.Feature({
        geometry: new ol.geom.LineString(lintCoor)
    })

    // lineFea.setProperties()
    pointLayer.getSource().addFeature(lineFea)
}

//绘制初始巡查路线
function drawInitLine(value) {
    let data = JSON.parse(value)
    let pointLayer = new ol.layer.Vector({
        source: new ol.source.Vector(),
        style: feature => {
            let attr = feature.A
            let status = attr.status
            let imageList = {
                "1": '../images/Patrol.png',//已巡查
                "2": '../images/missPatrol.png',//漏巡查
                "3": '../images/onPatrol.png',//正在巡查
                "4": '../images/noPatrol.png',//未巡查
            }
            let imgSrc = status && imageList[status] ? imageList[status] : imageList['4']
            // let imgSrc = '../images/noPatrol.png'
            let style = new ol.style.Style({
                image: new ol.style.Icon({
                    scale: 0.55,
                    // anchor: [0.5, 1],
                    opacity: 1,
                    src: imgSrc
                })
            })
            return style
        }
    })
    let lineLayer = new ol.layer.Vector({
        source: new ol.source.Vector(),
        style: feature => {
            // let attr = feature.values_.attribution
            // let sattus = attr.status
            let style = new ol.style.Style({
                stroke: new ol.style.Stroke({
                    color: '#86909c',
                    width: 2,
                    lineDash: [3, 4],
                })
            })
            return style
        }
    })
    let group = new ol.layer.Group({
        layers: [lineLayer, pointLayer]
    })
    state.map.getLayers().removeAt(state.layerIndex.initLineLayer)
    state.map.getLayers().insertAt(state.layerIndex.initLineLayer, group)
    let lintCoor = []
    if (data && data.length > 0) {

        data.forEach(item => {
            let coordinate = wgs84Tomercator([+item.lon, +item.lat])
            lintCoor.push(coordinate)
            let pointFea = new ol.Feature({
                geometry: new ol.geom.Point(coordinate)
            })
            pointFea.setProperties(item)
            pointLayer.getSource().addFeature(pointFea)
        });
    }
    let lineFea = new ol.Feature({
        geometry: new ol.geom.LineString(lintCoor)
    })
    // lineFea.setProperties()
    lineLayer.getSource().addFeature(lineFea)
    let extent = lineLayer.getSource().getExtent()
    extent && state.map.getView().fit(extent, {
        // padding:[20,20,20,20]
    })
    let zoom = state.map.getView().getZoom()
    if(zoom<10){
    state.map.getView().setZoom(10)
    }else
    state.map.getView().setZoom(zoom - 1)
}


//天地图通过xyz方式调用天地图
function getTileLayer_XYZ(type) {
    const TDT_Key = '870da0e1f75769017a577c93ff2be833'
    let layerTYPE_proJ = type + "_w";
    let url =
        "https://t{0-7}.tianditu.gov.cn/DataServer?T=" + layerTYPE_proJ + "&x={x}&y={y}&l={z}&tk=" + TDT_Key;
    let layer = new ol.layer.Tile({
        source: new ol.source.XYZ({
            url: url
        }),
        projection: 'EPSG:4326',
    })
    return layer
}
function getBlankLayers(num) {
    let BlankLayers = [];
    for (let i = 0; i < num; i++) {
        BlankLayers.push(new ol.layer.Vector({ visible: false }));
    }
    return BlankLayers;
}
function wgs84Tomercator(coor) {
    return ol.proj.transform(coor, 'EPSG:4326', 'EPSG:3857')
}
